package com.mk.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * TODO 未完成功能-_-
 * 资源类
 */
class Resources {
    private int num = 1; // A:1; B:2; C:3
    private Lock lock = new ReentrantLock();
    /* 相互于一把锁配了三把备用钥匙 */
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();

    /**
     * 打印
     * @param num 打印次数
     */
    public void print(int num) {
        lock.lock();
        try {
            String threadName = Thread.currentThread().getName();
            if (!threadName.equals("A")) {
                
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}


/**
 * Lock测试
 * 要求多个线程依然循环10次打印
 * A(1次) -> B(2次) -> c(3次)
 */
public class LockDemo {

    public static void main(String[] args) {
        Resources resources = new Resources();
        new Thread(() -> {
           for(int i = 1; i <= 1; i++) {
               resources.print(i);
           }
        }, "A").start();

        new Thread(() -> {
            for(int i = 1; i <= 2; i++) {
                resources.print(i);
            }
        }, "B").start();

        new Thread(() -> {
            for(int i = 1; i <= 3; i++) {
                resources.print(i);
            }
        }, "C").start();


    }
}
